package com.link.base.base.forum.controller;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.user.model.CoreUser;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.common.service.AttachmentService;
import com.link.base.base.forum.model.Forum;
import com.link.base.base.forum.model.ForumReplay;
import com.link.base.base.forum.model.People;
import com.link.base.base.forum.service.ForumService;
import com.link.base.base.image.model.Image;
import com.link.base.base.image.service.AppImageService;
import com.link.base.base.knowledge.model.Knowledge;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.query.Sorter;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.base.core.basic.util.YmlConstants;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛，加注释
 *
 * @author ouyangzhenqiang
 * @date 2018/11/7
 */
@Controller
@RequestMapping("/link/forum")
public class ForumController extends BasicController<Forum> {

    @Resource
    protected KeyGenerateService keyGenerateService;
    @Resource
    private ForumService forumService;
    @Resource
    private AppImageService appImageService;
    @Resource
    private AttachmentService attachmentService;

    @Override
    public BasicService<Forum> getBasicService() throws Exception {
        return forumService;
    }


    /**
     * 论坛列表查询
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumList")
    @ResponseBody
    public Map<String, Object> forumList(@JsonParam QueryParams qps, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        CoreUser user = UserUtil.getUser();
        String forumSearchValue1 = "";
        try {
            qps.setTotalFlag(true);
            qps.parseFromRaw();
            qps.addSorter(new Sorter("created", Sorter.DIR_DESC));
            String dataBase = "mysql";
            if (qps.getSearchParam() != null && !"".equals(qps.getSearchParam())) {
                if (dataBase.equals(YmlConstants.getApplicationDatabase())) {
                    forumSearchValue1 = qps.getSearchValue();
                } else {
                    qps.addFilter(new Filter("noteTitle||note||userName||platform||created||createdBy", Filter.OP_LIKE, qps.getSearchValue()));
                }
                qps.setSearchParam(null);
                qps.setSearchValue(null);
            }
            Forum t = (Forum) BasicModel.transformClass(Forum.class, qps);
            t.setCreatedBy(user.getId());
            if (dataBase.equals(YmlConstants.getApplicationDatabase())) {
                t.setForumSearchValue1(forumSearchValue1);
            }
            List<Forum> list = forumService.queryForumList(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
            result.put("totalPage", t.getTotalPage());

        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 新建论坛帖子
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumInsert", method = RequestMethod.POST)
    @ResponseBody
    public void forumInsert(@JsonParam Forum record, HttpSession session,
                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> myfiles = multipartRequest.getFiles("myfiles");
        try {
            CoreUser user = UserUtil.getUser();
            record.setUserName(String.valueOf(user.getId()));
            forumService.addForum(record);
            Attachment att = new Attachment();
            att.setHeadId(record.getId());
            //类型设置为附件对应的模块
            att.setModuleType("forum");
            for (MultipartFile file : myfiles) {
                att.setId(keyGenerateService.keyGenerate());
                attachmentService.upload(file, att);
                attachmentService.insert(att);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
            result.put("result", e.getMessage());
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            OutputStream os;
            response.setContentType("text/html;charset=UTF-8");
            os = response.getOutputStream();
            JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(os);
            jsonGenerator.writeObject(result);
        } catch (IOException e) {
            LCLogger.withException(e);
        }

    }

    /**
     * 更新论坛帖子
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumUpdate", method = RequestMethod.POST)
    @ResponseBody
    public void forumUpdate(@JsonParam Forum record, HttpSession session,
                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> myfiles = multipartRequest.getFiles("myfiles");
        try {
            CoreUser user = UserUtil.getUser();
            record.setUserName(user.getFirstName());
            forumService.updateForum(record);
            Attachment att = new Attachment();
            att.setHeadId(record.getId());
            //类型设置为附件对应的模块
            att.setModuleType("forum");
            for (MultipartFile file : myfiles) {
                att.setId(keyGenerateService.keyGenerate());
                attachmentService.upload(file, att);
                attachmentService.insert(att);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            OutputStream os;
            response.setContentType("text/html;charset=UTF-8");
            os = response.getOutputStream();
            JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(os);
            jsonGenerator.writeObject(result);
        } catch (IOException e) {
            LCLogger.withException(e);
        }

    }


    /**
     * 不带附件记录的帖子新建
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumInsertWithoutFile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forumInsertWithoutFile(@JsonParam Forum record, HttpSession session,
                                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            record.setUserName(String.valueOf(user.getId()));
            if (record.getNote() == null) {
                record.setNote("");
            }
            record.setReplayNum("0");
            forumService.addForum(record);
            result.put("success", true);
            result.put("row", record);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 生成论坛记录ID
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/generateForumId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> generateForumId(@JsonParam Forum record, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            record.setUserName(user.getFirstName());
            record.setId(keyGenerateService.keyGenerate());
            result.put("success", true);
            result.put("row", record);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取单个论坛记录
     *
     * @author 欧阳振强
     * @date 2019/12/30
     * @param id 论坛记录Id
     */
    @RequestMapping(value = "/getOneForum/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getOneForum(@PathVariable String id, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        CoreUser user = UserUtil.getUser();
        try {
            QueryParams qps = new QueryParams();
            qps.setPageFlag(false);
            qps.setId(Long.parseLong(id));
            Forum t = (Forum) BasicModel.transformClass(Forum.class, qps);
            t.setCreatedBy(user.getId());
            t.setId(Long.parseLong(id));
            Filter filter = new Filter("id", id);
            List<Filter> filterList = new ArrayList<Filter>();
            filterList.add(filter);
            t.setFilterList(filterList);
            List<Forum> forum = forumService.queryForumList(t);
            if (null != forum && forum.size() != 0) {
                result.put("result", forum.get(0));
                forumService.setStatisticsData(forum.get(0), user.getId());
            } else {
                //查不到主题帖算查询不成功
                result.put("success", false);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除单个论坛记录
     *
     * @author 欧阳振强
     * @date 2019/12/30
     * @param id 论坛记录Id
     */
    @RequestMapping(value = "/forumDelete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forumDelete(@PathVariable String id, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        CoreUser user = UserUtil.getUser();
        try {
            QueryParams qps = new QueryParams();
            qps.setPageFlag(false);
            qps.addFilter(new Filter("id", id));
            Forum t = (Forum) BasicModel.transformClass(Forum.class, qps);
            t.setCreatedBy(user.getId());
            List<Forum> forumList = forumService.queryForumList(t);
            Forum forum = forumList.get(0);
            if (!user.getId().equals(forum.getCreatedBy())) {
                throw new ServiceException("FORUM-001");
            }
            forumService.forumDelete(forum);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 获取帖子评论列表
     *
     * @author 欧阳振强
     * @date 2019/12/30
     * @param id 论坛记录Id
     */
    @RequestMapping(value = "/forumReplayList/{id}")
    @ResponseBody
    public Map<String, Object> forumReplayList(@PathVariable String id, @JsonParam QueryParams qps, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.parseFromRaw();
            qps.addFilter(new Filter("noteId", id));
            ForumReplay t = (ForumReplay) BasicModel.transformClass(ForumReplay.class, qps);
            List<ForumReplay> list = forumService.queryForumReplayPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 新建回复
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumReplyInsert", method = RequestMethod.POST)
    @ResponseBody
    public void forumReplyInsert(@JsonParam ForumReplay record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> myfiles = multipartRequest.getFiles("myfiles");
        try {
            CoreUser user = UserUtil.getUser();
            record.setUserName(user.getFirstName());
            forumService.addReplayOfForum(record);
            Attachment att = new Attachment();
            att.setHeadId(record.getId());
            att.setComments("");
            //类型设置为附件对应的模块
            att.setModuleType("forum");
            for (MultipartFile file : myfiles) {
                att.setId(keyGenerateService.keyGenerate());
                attachmentService.upload(file, att);
                attachmentService.insert(att);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            OutputStream os;
            response.setContentType("text/html;charset=UTF-8");
            os = response.getOutputStream();
            JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(os);
            jsonGenerator.writeObject(result);
        } catch (IOException e) {
            LCLogger.withException(e);
        }
    }

    /**
     * 查询主题可选的参与人
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/peopleList")
    @ResponseBody
    public Map<String, Object> peopleList(@JsonParam QueryParams qps, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.addSorter(new Sorter("created", Sorter.DIR_DESC));
            People t = (People) BasicModel.transformClass(People.class, qps);
            List<People> list = forumService.queryPeoplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
            result.put("totalPage", qps.getTotalPage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询主题已选的参与人
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/peopleOfForumList/{noteId}")
    @ResponseBody
    public Map<String, Object> peopleOfForumList(@PathVariable String noteId,@JsonParam QueryParams qps, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.addSorter(new Sorter("created", Sorter.DIR_DESC));
            qps.addFilter(new Filter("noteId", noteId));
            People t = (People) BasicModel.transformClass(People.class, qps);
            List<People> list = forumService.queryPeopleOfForumByPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 插入主题参与人
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumPeopleInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forumPeopleInsert(@RequestBody Forum record, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            forumService.forumPeopleInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除主题参与人
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @RequestMapping(value = "/forumPeopleDelete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forumPeopleDelete(@RequestBody Forum record, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            forumService.forumPeopleDelete(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询帖子各类型总数
     *
     * @author lizt
     * @data 2017年4月26日
     */
    @RequestMapping(value = "/queryForumTypeNum", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryForumTypeNum(@JsonParam Forum record, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Forum> list = forumService.queryForumTypeNum(record);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }

    /**
     * 帖子查询 根据条件查询记录,默认分页(若将'pageFlag'设置为'false'，则不分页)
     * 代替app端 link/GuideForum/queryByExamplePage
     * @author ouyangzhenqiang
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    @RequestMapping(value = "/queryByExamplePage")
    @ResponseBody
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, @JsonParam Forum entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        LinkRedisClient jedis = RedisUtil.getJedis();
        CoreUser user = UserUtil.getUser();
        try {
            qps.addSorter(new Sorter("created", Sorter.DIR_DESC));
            Forum t = (Forum) BasicModel.transformClass(entity, qps);
            List<Forum> list = forumService.queryByExamplePage(t);
            // 获取帖子相关一些内容
            for (Forum i : list) {
                // 获取帖子相关图片
                Image imageItem = new Image();
                imageItem.setParentid(i.getId());
                imageItem.setModule("forum");
                List<Image> imageList = appImageService.queryAll(imageItem);
                i.setImageList(imageList);
                forumService.setStatisticsData(i, user.getId());
            }
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Knowledge>());
            result.put("result", e.getMessage());
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return result;
    }

    /**
     * queryById
     *
     * @author 欧阳振强
     * @date 2019/12/30
     */
    @Override
    @RequestMapping(value = "/queryById")
    @ResponseBody
    public Map<String, Object> queryById(@JsonParam Forum entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            Forum record = forumService.queryById(entity);
            forumService.setStatisticsData(record, user.getId());
            record.setAnonymousFlag("Y");
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 分享内容
     * @author tangjian
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryShareById")
    @ResponseBody
    public Map<String, Object> queryShareById(@JsonParam Forum entity, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Forum record = forumService.queryById(entity);
            // 获取帖子相关图片
            Image imageItem = new Image();
            imageItem.setAnonymousFlag("Y");
            imageItem.setParentid(record.getId());
            imageItem.setModule("forum");
            List<Image> imageList = appImageService.queryAll(imageItem);
            for (Image i : imageList) {
                i.setAnonymousFlag("Y");
            }
            record.setImageList(imageList);
            forumService.setStatisticsData(record, null);
            record.setAnonymousFlag("Y");
            QueryParams qps = new QueryParams();
            qps.parseFromRaw();
            qps.addFilter(new Filter("noteId", String.valueOf(record.getId())));
            ForumReplay t = (ForumReplay) BasicModel.transformClass(ForumReplay.class, qps);
            t.setAnonymousFlag("Y");
            t.setPageFlag(false);
            List<ForumReplay> list = forumService.queryForumReplayPage(t);
            for (ForumReplay f : list) {
                f.setAnonymousFlag("Y");
            }
            result.put("result", record);
            result.put("replyList", list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }



    /**
     * 删除回复
     *
     * @param forumReplay
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delForumReplyById", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> delForumReplyById(@JsonParam ForumReplay forumReplay, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            forumService.delForumReplyById(forumReplay);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * app论坛模块，语音上传
     *
     * @param multiFile
     * @param request
     * @param respone
     * @return
     */
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST, produces = "application/json")
    public Map<String, Object> fileUpload(@RequestParam("file") MultipartFile multiFile, // 获取的MultipartFile文件，参数为'file',
                                          HttpServletRequest request, HttpServletResponse respone) {
        Long replyId = Long.parseLong(request.getParameter("replyId"));
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Attachment record = new Attachment();
            record.setModuleType("forum");
            record.setSource("app");
            record.setHeadId(replyId);
            attachmentService.upload(multiFile, record);
            Attachment attachment = attachmentService.queryById(replyId);
            map.put("success", "true");
            map.put("attachment", attachment);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("result", e.getMessage());
            map.put("success", false);
        }
        return map;
    }

    /**
     * 用于营销助手App发布帖子
     * @param forumNote
     * @param request
     * @param response
     * @return result
     */
    @RequestMapping(value = "/forumNotePost", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forumNotePost(@RequestBody Forum forumNote, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            forumNote.setUserName(String.valueOf(user.getId()));
            if (forumNote.getNote() == null) {
                forumNote.setNote("");
            }
            forumNote.setReplayNum("0");
            forumService.insert(forumNote);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }


    /**
     * 回帖查询
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryForumReplayList", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryForumReplayList(HttpSession session, HttpServletRequest request,
                                                    HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        int count = 0;
        int currentCount = Integer.parseInt(request.getParameter("currentCount"));
        // 帖子Id
        String id = request.getParameter("id");
        // 是否只看自己的评论
        String isMe = request.getParameter("isMe");
        CoreUser user = UserUtil.getUser();
        ForumReplay fr = new ForumReplay();
        fr.setNoteId(Long.parseLong(id));
        fr.setIsMe(isMe);
        try {
            if (currentCount == 0) {
                count = forumService.queryForumReplayListCount(fr);
            }
            List<ForumReplay> list = forumService.queryForumReplayList(fr);
            Image image = null;
            Attachment attachment = null;
            for (ForumReplay f : list) {
                if (user.getId().equals(f.getCreatedBy())) {
                    f.setIsMe("Y");
                }
                // 查询图片信息
                image = new Image();
                Long headId = f.getId();
                image.setParentid(headId);
                image.setModule("forum");
                f.setImageList(appImageService.queryAll(image));
                // 音频附件
                attachment = attachmentService.queryVoiceAttachmentByHeadId(headId);
                f.setAttachment(attachment);
            }
            result.put("success", true);
            result.put("count", count);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }


    /**
     * 评论帖子
     * @param forumReplay
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/forumReplay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> forumReplay(@RequestBody ForumReplay forumReplay, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            forumReplay.setIsMe("");
            forumService.forumReplayInsert(forumReplay);
            String image = "image";
            if (image.equals(forumReplay.getReplyType())) {
                // 更新图片头Id
                List<Image> images = forumReplay.getImageList();
                if (images != null && images.size() > 0) {
                    for (int i = 0; i < images.size(); i++) {
                        images.get(i).setParentid(forumReplay.getId());
                        appImageService.imageHeadIdUpdate(images.get(i));
                    }
                }
                // 获取图片附件
                Image i = new Image();
                i.setParentid(forumReplay.getId());
                i.setModule("forum");
                List<Image> imageList = appImageService.queryAll(i);
                CoreUser user = UserUtil.getUser();
                Long userId = user.getId();
                forumReplay.setUserId(userId);
                forumReplay.setUserName(user.getFirstName());
                forumReplay.setImageList(imageList);
            }
            // 获取语音附件
            String voice = "voice";
            if (voice.equals(forumReplay.getReplyType())) {
                Attachment attachment = attachmentService.queryVoiceAttachmentByHeadId(forumReplay.getId());
                forumReplay.setAttachment(attachment);
            }
            result.put("success", true);

        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }

}
